<template>
  <div class="app-container">
    <el-form :model="dataForm" class="cus_form">
      <el-row :gutter="20">
        <el-col :span="14">
          <el-form-item>
            <el-input v-model="dataForm.userName" size="mini" placeholder="用户名/邮箱/姓名" clearable>
              <el-button slot="append" icon="el-icon-search" @click="getDataList()" />
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="10" style="text-align: right; ">
          <el-button icon="el-icon-plus" size="mini" class="cla-user-add" type="primary" @click="addOrUpdateHandle()">
            新建
          </el-button>
            &nbsp;&nbsp;
        </el-col>
      </el-row>
    </el-form>
    <el-table v-loading="dataListLoading" :data="dataList" border class="eltable" :height="viewHeight" style="width: 100%;">
      <el-table-column type="selection" header-align="left" align="left" width="30" />
      <el-table-column v-if="false" prop="userId" header-align="left" align="left" width="80" label="ID" />
      <el-table-column prop="Username" header-align="left" align="left" min-width="140" label="用户名" />
      <el-table-column prop="Name" header-align="left" align="left" min-width="80" label="姓名" />
      <el-table-column prop="PositionName" show-overflow-tooltip header-align="left" align="left" min-width="100" label="岗位" />
      <el-table-column prop="OrgName" header-align="left" align="left" show-overflow-tooltip min-width="180" label="组织" />
      <el-table-column prop="TenantName" header-align="left" align="left" min-width="100" show-overflow-tooltip label="租户" />
      <el-table-column prop="Staffnum" header-align="left" align="left" min-width="100" label="采购员代码" />
      <el-table-column prop="Email" header-align="left" align="left" min-width="200" label="邮箱" />
      <el-table-column prop="Phone" header-align="left" align="left" min-width="120" label="手机号" />
      <el-table-column prop="status" header-align="center" align="center" width="80" label="状态">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status === 0" size="small" type="danger">禁用</el-tag>
          <el-tag v-else size="small">正常</el-tag>
        </template>
      </el-table-column>
      <el-table-column fixed="right" header-align="center" align="center" width="280" label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" icon="el-icon-edit" @click="modifyhandler(scope)">&nbsp;修改
          </el-button>
          <el-button type="text" size="small" icon="el-icon-delete" @click="modifyhandler(scope)">
            删除
          </el-button>
          <el-button type="text" size="small" icon="el-icon-plus" @click="modifyhandler(scope)">
            角色
          </el-button>
          <el-button type="text" size="small" icon="el-icon-setting" @click="genpwdhandler(scope.row)">
            生成密码
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-row justify="end">
      <el-col style="text-align: right;">
        <el-pagination :current-page="pageIndex" :page-sizes="[10, 20, 50, 100]" :page-size="pageSize" :total="totalPage" layout="total, sizes, prev, pager, next, jumper" />
      </el-col>
    </el-row>
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList()" />
  </div>
</template>
<script>
import store from '@/store'
import { fetchList, genpwd } from '@/api/user'
import { fetchAll } from '@/api/tenant'
import { fetchList as fetchPositionList } from '@/api/position'
import { fetchList as fetchOrgList } from '@/api/org'
import AddOrUpdate from './components/addorupdate'
export default {
  components: {
    AddOrUpdate
  },
  data() {
    return {
      dataForm: {
        userName: ''
      },
      addOrUpdateVisible: false,
      viewHeight: store.getters.clientHeight - 210,
      dataListSelections: [],
      dataList: [],
      dataListLoading: false,
      pageIndex: 1,
      pageSize: 10,
      totalPage: 1,
      tenantList: [],
      orgList: [],
      positionList: []
    }
  },
  mounted() {
    // 获取租户
    fetchAll().then((data) => {
      if (data && data.code === '20000') {
        const result = data.data
        this.tenantList = result
      }
    }).then(() => {
      fetchOrgList().then((data) => {
        if (data && data.code === '20000') {
          const result = data.data
          this.orgList = result
        }
      }).then(() => {
        fetchPositionList().then((data) => {
          if (data && data.code === '20000') {
            const result = data.data
            this.positionList = result
          }
        }).then(() => {
          this.getDataList()
        })
      })
    })
  },
  methods: {
    getDataList() {
      const condition = {
        limit: this.pageSize,
        offset: this.pageSize * (this.pageIndex - 1)
      }
      const searchcondition = this.dataForm.userName
      if (searchcondition) {
        condition.query = `Email.contains:${searchcondition},Condition_Join:or,Username.contains:${searchcondition},Condition_Join:or,Name.contains:${searchcondition}`
      }
      fetchList(condition).then((data) => {
        if (data.data && data.data.length > 0) {
          data.data.forEach((item, index) => {
            let re = this.tenantList.filter(r => r.Id === item.Tenant)
            if (re && re.length > 0) {
              item.TenantName = re[0].Name
            } else {
              item.TenantName = ''
            }
            re = this.orgList.filter(r => r.Id === item.Orgid)
            if (re && re.length > 0) {
              item.OrgName = re[0].Name
            } else {
              item.OrgName = ''
            }
            re = this.positionList.filter(r => r.Id === item.Positionid)
            if (re && re.length > 0) {
              item.PositionName = re[0].Name
            } else {
              item.PositionName = ''
            }
          })
          this.dataList = data.data
          this.totalPage = data.count % this.pageSize === 0 ? (data.count / this.pageSize) : (Math.floor(data.count / this.pageSize) + 1)
          this.total = data.count
        } else {
          this.element.dataList = []
        }
      })
    },
    modifyhandler(scope) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(scope.row.Id)
      })
    },
    addOrUpdateHandle(id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id)
      })
    },
    genpwdhandler(row) {
      this.$confirm(`确定对[Name=${row.Name}]进行[生成密码]操作?新生成的密码将会覆盖原先的密码，并以邮件的方式发动到用户邮箱中，请注意查收!!!`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        genpwd(row.Id).then((data) => {
          if (data && data.code === '20000') {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.getDataList()
              }
            })
          } else {
            this.$message.error(data.msg)
          }
        })
      }).catch(() => {})
    }
  }
}
</script>
